package com.ftg.learn;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author Guo
 * @date 2022/5/27
 */
public class Reverse1 {


    /**
     * 数据消费者
     * @param args
     */
    public static void main(String[] args) {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("localhost");
        try (
                Connection c = cf.newConnection();
                Channel channel = c.createChannel()
        ){

            channel.queueDeclare("test1",false,false,false,null);

            // 从指定队列中取出数据
            // true 代表消费完成之后清空队列。
            channel.basicConsume("test1",true,(x,y)->{
                String str = new String(y.getBody(), "utf-8");
                try{
                    doWork(str);
                }catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println("str ==>" + str);
            },x->{});
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }

    private static void doWork(String str) throws InterruptedException {
        for (int i = 0;i < str.length();i++){
            System.out.println(str.charAt(i));
            Thread.sleep(2000);
        }
    }
}
